package susano.demo.action.company;

import java.io.InputStream;
import java.util.logging.Logger;

import org.apache.commons.lang.StringUtils;
import org.apache.tools.ant.filters.StringInputStream;

import susano.presen.controller.Action;
import susano.presen.controller.Navigation;
import susano.presen.util.DownloadUtil;

public class DownloadAction extends Action {

	protected static Logger logger = Logger.getLogger(DownloadAction.class
			.getName());

	@Override
	protected Navigation execute() {
		download();
		return null;
	}

	@Override
	protected Navigation init() {
		download();
		return null;
	}

	@Override
	protected Navigation validate() {
		return null;
	}

	private void download() {
		String type = param("type");
		if (StringUtils.isBlank(type)) {
			return;
		}
		if (type.equals("text")) {
			downloadText();
		} else if (type.equals("image")) {
			downloadImage();
		}
	}

	private void downloadText() {
		DownloadUtil.download(request, response,
				new StringInputStream("あああああ。", "UTF-8"), "あああ.txt");
	}

	private void downloadImage() {
		String filename = "appengine_lowres.gif";
		InputStream is = getClass().getClassLoader().getResourceAsStream(
				filename);
		DownloadUtil.download(request, response, is, filename);
	}
}
